%%

% ***********************************************
% Load Data Set
% ***********************************************
dataset = 'ORL_1024';
load(dataset);
[nDim, nSmp] = size(X);
nClusters = length(unique(y));
assert(nClusters == max(y));

%%

% ***********************************************
% Run FNMTF
% ***********************************************

% Parameters
nFeaClusters = nClusters;
nSmpClusters = nClusters;

nRun = 50;

FNMTF_result = [];
FNMTF_result.dataset = dataset;
FNMTF_result.objHistory = cell(nRun,1); %
FNMTF_result.iterHistory = zeros(nRun,1); %
FNMTF_result.timeHistory = zeros(nRun,1); %
FNMTF_result.nmiHistory = zeros(nRun,2); %
FNMTF_result.accHistory = zeros(nRun,1); %
FNMTF_result.purityHistory = zeros(nRun,1); %

for iRun = 1:nRun;
    str = sprintf( 'Trying %d out of %d', iRun, nRun );
    disp(str);
    
    tic;
    [F, G, obj] = FNMTF(X, nSmpClusters, nFeaClusters);
    timeUsed = toc;
    
    FNMTF_result.objHistory{iRun,1} = obj;
    FNMTF_result.iterHistory(iRun,1) = length(obj);
    FNMTF_result.timeHistory(iRun,1) = timeUsed;
    
    [~, idx] = max(F, [], 2);
    [~, ~, result] = evalClustering(y, idx);
    
    FNMTF_result.nmiHistory(iRun,:) = result.nmi_max;
    FNMTF_result.accHistory(iRun,1) = result.acc;
    FNMTF_result.purityHistory(iRun,1) = result.purity;
end

%%

% ***********************************************
% Run LPFNMTF
% ***********************************************

% Parameters
nFeaClusters = nClusters;
nSmpClusters = nClusters;
W_f = constructW(X', struct('Metric', 'Euclidean', 'NeighborMode', 'KNN', 'k', 5, 'WeightMode', 'HeatKernel'));
lamda1 = 100;
W_d = constructW(X, struct('Metric', 'Euclidean', 'NeighborMode', 'KNN', 'k', 5, 'WeightMode', 'HeatKernel'));
lamda2 = 100;

nRun = 1;

LPFNMTF_result = [];
LPFNMTF_result.dataset = dataset;
LPFNMTF_result.objHistory = cell(nRun,1); %
LPFNMTF_result.iterHistory = zeros(nRun,1); %
LPFNMTF_result.timeHistory = zeros(nRun,1); %
LPFNMTF_result.nmiHistory = zeros(nRun,2); %
LPFNMTF_result.accHistory = zeros(nRun,1); %
LPFNMTF_result.purityHistory = zeros(nRun,1); %

for iRun = 1:nRun;
    str = sprintf( 'Trying %d out of %d', iRun, nRun );
    disp(str);
    
    tic;
    [F, G, obj] = LPFNMTF(X', nFeaClusters, nSmpClusters, W_f, lamda1, W_d, lamda2);
    timeUsed = toc;
    
    LPFNMTF_result.objHistory{iRun,1} = obj;
    LPFNMTF_result.iterHistory(iRun,1) = length(obj);
    LPFNMTF_result.timeHistory(iRun,1) = timeUsed;
    
    [~, idx] = max(G, [], 2);
    [~, ~, idx] = unique(idx);
    [~, ~, result] = evalClustering(y, idx);
    LPFNMTF_result.nmiHistory(iRun,:) = result.nmi_max;
    LPFNMTF_result.accHistory(iRun,1) = result.acc;
    LPFNMTF_result.purityHistory(iRun,1) = result.purity;
end
[mean(LPFNMTF_result.accHistory),mean(LPFNMTF_result.nmiHistory), mean(LPFNMTF_result.purityHistory)]